#!/usr/bin/env python
# -*- coding: utf-8 -*

import os,commands
from modules.files import *
from modules.auto_ssh import *

#需要修改的配置
######################################
user = 'root'
password='TDXceTN+mUuu6Y='
home='/var/www/html/python_intall'
zk_version='zookeeper-3.4.14'
zkconf='zk.conf'
zkfile="%s/conf/%s" %(home,zkconf)
######################################

port=22
sign='*' * 30  + '\n'
install_log='%s/logs/zk_install.log' % (home)
src_zk='%s/soft/%s' %(home,zk_version)
zoo='%s/soft/%s/conf/zoo.cfg' % (home,zk_version)
zk_home='/home/hadoop/apache/zookeeper'
exp_zkhome='''echo -e '\nexport ZOOKEEPER_HOME=/home/hadoop/apache/zookeeper/latest' >> /etc/profile '''
exp_zklogdir='''echo -e 'export ZOO_LOG_DIR=$ZOOKEEPER_HOME/logs' >> /etc/profile '''
exp_zkpath='''echo -e 'PATH=$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/sbin:$PATH' >> /etc/profile '''
ln = 'ln -s %s/%s %s/latest' %(zk_home,zk_version,zk_home)
zkstart="su - hadoop -c 'cd %s ; ./latest/bin/zkServer.sh start'" %(zk_home)
zkstop="su - hadoop -c 'cd %s ; ./latest/bin/zkServer.sh stop'" %(zk_home)
zkstatus="su - hadoop -c 'cd %s ; ./latest/bin/zkServer.sh status'" %(zk_home)
rmr="su - hadoop -c 'cd %s ; ./latest/bin/zkServer.sh stop;rm -rf /home/hadoop/apache' " %(zk_home)
ZKSER=''
DATADIR=''
DATALOG=''
zk_list=[]
chown='chown hadoop:hadoop -R /home/hadoop'
zoo_demo='''tickTime=6000
initLimit=20
syncLimit=5
syncLimit=20
clientPort=2181
maxClientCnxns=9000
dataDir=DATADIR
dataLogDir=DATALOG
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
kerberos.removeHostFromPrincipal=true
kerberos.removeRealmFromPrincipal=true
jaasLoginRenew=3600000
skipACL=yes'''

if not os.path.exists(src_zk):
    tar='/usr/bin/tar zxf %s.tar.gz -C %s/soft/' %(src_zk,home)
    #clean_zkdir='rm -rf %s/soft/%s' % (home,zk_version)
    #commands.getoutput(clean_zkdir)
    commands.getoutput(tar)
##生成配置文件
for line in File().read(zkfile):
    if line.split('=')[0] == 'ZKSER':
        ZKSER = line.split('=')[-1].strip()
        zk_list = ZKSER.split(',')
    elif line.split('=')[0] == 'DATADIR':
        DATADIR = line.split('=')[-1].strip()
    elif line.split('=')[0] == 'DATALOG':
        DATALOG= line.split('=')[-1].strip()

mkdir='test -d %s || mkdir -p %s;chown hadoop:hadoop -R %s' % (DATADIR,DATADIR,DATADIR)
mklog='test -d %s || mkdir -p %s;chown hadoop:hadoop -R %s' % (DATALOG,DATALOG,DATALOG)


## The  First step
## Generate configuration file
#File().write(zoo,zoo_demo)
#File().fileinput(zoo,'DATADIR',DATADIR)
#File().fileinput(zoo,'DATALOG',DATALOG)
#for id in xrange(len(zk_list)):
#    svr = "server.%d=%s:2888:3888\n" %((id+1),zk_list[id])
#    File().add(zoo,svr)
#
#File().add(install_log,'Default zoo.cfg config : \n\n')
#conflog=File().read(zoo)
#for line in conflog:
#    File().add(install_log,line)
#
#File().add(install_log,sign)


## The Second step
## zookeeper 集群节点初始化 环境变量
#for id in xrange(len(zk_list)):
#    Auto_ssh().execute_key(zk_list[id],user,mkdir)
#    Auto_ssh().execute_key(zk_list[id],user,mklog)
#    zk_id="echo %d > %s/myid" % ((id+1),DATADIR)
#    Auto_ssh().execute_key(zk_list[id],user,zk_id)
#    Ser_info = '%s\tmyid\t%s\n' %(zk_list[id],(id+1))
#    File().add(install_log,Ser_info)
#    scp = 'scp -r %s root@%s:%s' %(src_zk,zk_list[id],zk_home)
#    commands.getoutput(scp)
#    Auto_ssh().execute_key(zk_list[id],user,exp_zkhome)
#    Auto_ssh().execute_key(zk_list[id],user,exp_zklogdir)
#    Auto_ssh().execute_key(zk_list[id],user,exp_zkpath)
#    Auto_ssh().execute_key(zk_list[id],user,ln)
#    Auto_ssh().execute_key(zk_list[id],user,chown)
#    File().add(install_log,sign)

#time.sleep(10)
## The Third step
for id in xrange(len(zk_list)):
    File().add(install_log,(zk_list[id]+'\n'))
    #Auto_ssh().execute_key(zk_list[id],user,zkstart)
    #time.sleep(5)
    File().add(install_log,(Auto_ssh().execute_key(zk_list[id],user,zkstatus)))
    File().add(install_log,sign)
    #Auto_ssh().execute_key(zk_list[id],user,zkstop)
#    #Auto_ssh().execute_key(zk_list[id],user,rmr)
